/**
 * tarside-ftp -- A simple FTP client, using the ANSI C and GTK+.
 * Copyright (C) 2011  degui.chen <degui.chen@qq.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "login.h"
#include "list.h"
#include "info.h"
static GtkWidget *host;
static GtkWidget *username;
static GtkWidget *password;
static GtkWidget *port;
static GtkWidget *model;


int fd_connect = 0;

void model_hander(GtkWidget *widget, gpointer *data) {
	if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(data))) {
		set_ftp_port_model();
		set_commond_info("set ftp port model\n");
	} else {
		set_ftp_pasv_model();
		set_commond_info("set ftp pasv model\n");
	}
}

static void button_clicked(GtkWidget *widget, gpointer data) {
	char buffer[1024] = { '\0' };
	char comment[1024] = { '\0' };
	fd_connect = open_connect(gtk_entry_get_text(host), atoi(gtk_entry_get_text(port)));
	recv_cmd(fd_connect, buffer, 1024);
	set_commond_info(buffer);

	sprintf(comment, "USER %s\r\n", gtk_entry_get_text(username));
	send_cmd(fd_connect, comment);
	set_commond_info(comment);
	recv_cmd(fd_connect, buffer, 1024);
	set_commond_info(buffer);

	sprintf(comment, "PASS %s\r\n", gtk_entry_get_text(password));
	send_cmd(fd_connect, comment);
	set_commond_info(comment);
	recv_cmd(fd_connect, buffer, 1024);
	set_commond_info(buffer);
	//
	//	char cwd[] = "CWD degui\r\n";
	//	send_cmd(fd_connect, cwd);
	//	recv_cmd(fd_connect, buffer, 1024);
	//	set_commond_info(buffer);

	init_remote_dir(fd_connect);
}

static void login_input(GtkWidget *hbox) {
	GtkWidget *host_label = gtk_label_new("Host:");
	host = gtk_entry_new();
	gtk_entry_set_text(host, "");
	gtk_container_add(GTK_CONTAINER(hbox), host_label);
	gtk_container_add(GTK_CONTAINER(hbox), host);

	GtkWidget *username_label = gtk_label_new("Username:");
	username = gtk_entry_new();
	gtk_entry_set_text(username, "");
	gtk_container_add(GTK_CONTAINER(hbox), username_label);
	gtk_container_add(GTK_CONTAINER(hbox), username);

	GtkWidget *password_label = gtk_label_new("Password:");
	password = gtk_entry_new();
	gtk_entry_set_visibility(GTK_ENTRY(password), FALSE);
	gtk_entry_set_text(password, "");
	gtk_container_add(GTK_CONTAINER(hbox), password_label);
	gtk_container_add(GTK_CONTAINER(hbox), password);

	GtkWidget *port_label = gtk_label_new("Port:");
	port = gtk_entry_new();
	gtk_entry_set_text(port, "21");
	gtk_container_add(GTK_CONTAINER(hbox), port_label);
	gtk_container_add(GTK_CONTAINER(hbox), port);

	model = gtk_check_button_new_with_label("Port Model");
	gtk_signal_connect(GTK_OBJECT(model), "toggled", GTK_SIGNAL_FUNC(model_hander), (gpointer *) model);
	gtk_container_add(GTK_CONTAINER(hbox), model);

	GtkWidget *connent = gtk_button_new_with_label("Connent");
	gtk_container_add(GTK_CONTAINER(hbox), connent);

	g_signal_connect(G_OBJECT(connent), "clicked", G_CALLBACK(button_clicked), NULL);

	GtkWidget *valign1 = gtk_alignment_new(1, 0, 0, 0);
	gtk_box_pack_start(GTK_BOX(hbox), valign1, FALSE, FALSE, 0);
}
void set_commond_info(const char* text) {
	printf(text);
	GtkTextIter start, end;
	gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(message_buffer), &start, &end);
	gtk_text_buffer_insert(message_buffer, &end, text, -1);
}
void login(GtkWidget *paned) {
	extern int main_height;
	int height = main_height / 4;
	GtkWidget *vbox = gtk_vbox_new(FALSE, 1);

	GtkWidget *input = gtk_hbox_new(FALSE, 1);
	login_input(input);
	gtk_container_add(GTK_CONTAINER(vbox), input);

	GtkWidget *scrolledWindow = gtk_scrolled_window_new(NULL, NULL);/*创建滚动窗口构件*/
	gtk_widget_show(scrolledWindow);
	GtkWidget* message = gtk_text_view_new();
	gtk_widget_set_size_request(vbox, 80, height);
	gtk_widget_set_size_request(scrolledWindow, 80, height - 21);
	message_buffer = gtk_text_buffer_new(NULL);
	gtk_text_view_set_buffer(message, message_buffer);
	const gchar *text = "ftp client info:\n";
	gtk_text_buffer_set_text(message_buffer, text, -1);
	gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolledWindow), message);
	gtk_container_add(GTK_CONTAINER(vbox), scrolledWindow);

	GtkWidget *valign = gtk_alignment_new(0, 1, 0, 0);
	gtk_box_pack_start(GTK_BOX(vbox), valign, FALSE, FALSE, 0);
	gtk_paned_add1(GTK_PANED(paned), vbox);/*将滚动窗口构件加入到分栏窗口构件的左窗口*/
}
